package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.example.common.core.utils.MapstructUtils;
import org.example.common.core.utils.page.Pager;
import org.example.common.datasource.utils.SearchParamsHelper;
import org.example.system.domain.SysOperatorLog;
import org.example.system.domain.bo.SysOperatorLogBo;
import org.example.system.domain.vo.SysOperatorLogVo;
import org.example.system.mapper.SysOperatorLogMapper;
import org.example.system.service.ISysOperatorLogService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 操作日志 服务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class SysOperatorLogServiceImpl implements ISysOperatorLogService {

    private final SysOperatorLogMapper baseMapper;


    @Override
    public Pager<SysOperatorLogVo> selectPageOperLogList(Pager pager) {
        SearchParamsHelper.loadSearchParam(pager, Wrappers.query(),(p,w)->baseMapper.selectVoPage(p,w));
        return pager;
    }

    @Override
    public List<SysOperatorLogVo> selectOperLogList(Pager pager) {
        QueryWrapper queryWrapper= Wrappers.query();
        SearchParamsHelper.loadSearchParam(pager, queryWrapper);
        return baseMapper.selectVoList(queryWrapper);
    }

    /**
     * 新增操作日志
     *
     * @param bo 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperatorLogBo bo) {
        SysOperatorLog operLog = MapstructUtils.convert(bo, SysOperatorLog.class);
        operLog.setOperTime(new Date());
        baseMapper.insert(operLog);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperatorLogVo selectOperLogById(Long operId) {
        return baseMapper.selectVoById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.delete(new LambdaQueryWrapper<>());
    }
}
